//
//  HMEmoticonToolBar.swift
//  Weibo22
//
//  Created by Apple on 16/12/10.
//  Copyright © 2016年 Apple. All rights reserved.
//

import UIKit

//  toolbar按钮对应的枚举类型
enum HMEmoticonToolBarButtonType: Int {
    //  最近表情
    case recent = 100
    //  默认表情
    case normal = 101
    //  emoji
    case emoji = 102
    //  浪小花
    case lxh = 103
}

//  表情键盘toolbar
class HMEmoticonToolBar: UIStackView {
    
    //  上一次选中的按钮
    var lastSelectedButton: UIButton?
    //  点击toolbar按钮执行的闭包
    var callBack: ((HMEmoticonToolBarButtonType)->())?

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        //  水平布局
        axis = .horizontal
        //  子控件填充方式
        distribution = .fillEqually
        
        addChildButton(bgImageName: "compose_emotion_table_left", title: "最近", type: .recent)
        addChildButton(bgImageName: "compose_emotion_table_mid", title: "默认", type: .normal)
        addChildButton(bgImageName: "compose_emotion_table_mid", title: "Emoji", type: .emoji)
        addChildButton(bgImageName: "compose_emotion_table_right", title: "浪小花", type: .lxh)
    }
    //  添加子按钮
    private func addChildButton(bgImageName: String, title: String, type: HMEmoticonToolBarButtonType) {
        let button = UIButton()
        //  把枚举的原始值作为按钮的tag使用
        button.tag = type.rawValue
        button.addTarget(self, action: #selector(btnAction(btn:)), for: .touchUpInside)
        //  设置不同状态的背景图片
        button.setBackgroundImage(UIImage(named: "\(bgImageName)_normal"), for: .normal)
        button.setBackgroundImage(UIImage(named: "\(bgImageName)_selected"), for: .selected)
        //  设置标题
        button.setTitle(title, for: .normal)
        //  设置不同状态下的背景色
        button.setTitleColor(UIColor.white, for: .normal)
        button.setTitleColor(UIColor.gray, for: .selected)
        //  设置字体大小
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        //  取消高亮
        button.adjustsImageWhenHighlighted = false
        //  添加控件
        addArrangedSubview(button)
        
        if type == .normal {
            lastSelectedButton?.isSelected = false
            button.isSelected = true
            lastSelectedButton = button
        }
        
    
    }
    
    @objc private func btnAction(btn: UIButton) {
        //  如果上次选中的按钮和点击的按钮相同,那么后面的代码不执行
        if lastSelectedButton == btn {
            return
        }
        
        lastSelectedButton?.isSelected = false
        btn.isSelected = true
        lastSelectedButton = btn
        
        //  获取点击按钮的枚举
        let type = HMEmoticonToolBarButtonType(rawValue: btn.tag)!
        //  执行闭包
        callBack?(type)
        
    
    }
    
    //  根据传入的section获取对应的按钮,让其选中
    func selectedButton(section: Int) {
        //  如果tag是0取得的视图是当前视图自己,不是这个按钮本身,不建议大家使用0作为视图的tag
        let button = viewWithTag(section + 100) as! UIButton
        //  如果上次选中的按钮和这次滚动要选中按钮相同,就直接返回不让其再次选中
        if lastSelectedButton == button {
            return
        }
        
        lastSelectedButton?.isSelected = false
        button.isSelected = true
        lastSelectedButton = button
        
        
    
    }
    
    
}
